﻿using KINLO.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KINLO.Data.Reporitory
{
    public class FrontWeightReporitory
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(WeightReporitory));
        Database DB;
        public FrontWeightReporitory()
        {
            DB = (new DbContext("name=WeightDB")).Database;
        }
        public FrontWeight GetFrontWeightByPLotNo(string lotNo)
        {
            try
            {
                var cmd = @"SELECT SystemAutoID ,Eno ,PLotNo ,PreTime ,PreWeight ,Result ,LimitsMin ,LimitsMax FROM dbo.FrontWeight
                             WHERE PLotNo=@PLotNo";
               return DB.SqlQuery<FrontWeight>(cmd, new SqlParameter("@PLotNo", lotNo)).FirstOrDefault();
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "FrontWeightReporitory", "GetFrontWeightByPLotNo", ex));
                return null;
            }
        }

        public int AddFrontWeight(FrontWeight w)
        {
            try
            {
                var cmd = @"INSERT dbo.FrontWeight
                               ( Eno ,PLotNo ,PreTime,PreWeight,Result ,LimitsMin ,LimitsMax)
                       VALUES  ( @Eno,@PLotNo,@PreTime,@PreWeight,@Result,@LimitsMin,@LimitsMax)";
                return DB.ExecuteSqlCommand(cmd,
                     new SqlParameter("@Eno", w.Eno == null ? DBNull.Value : (object)w.Eno),
                     new SqlParameter("@PLotNo", w.PLotNo == null ? DBNull.Value : (object)w.PLotNo),
                     new SqlParameter("@PreTime", w.PreTime == null ? DBNull.Value : (object)w.PreTime),
                     new SqlParameter("@PreWeight", w.PreWeight == null ? DBNull.Value : (object)w.PreWeight),
                     new SqlParameter("@Result", w.Result == null ? DBNull.Value : (object)w.Result),
                     new SqlParameter("@LimitsMin", w.LimitsMin == null ? DBNull.Value : (object)w.LimitsMin),
                     new SqlParameter("@LimitsMax", w.LimitsMax == null ? DBNull.Value : (object)w.LimitsMax)
                     );
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "FrontWeightReporitory", "AddFrontWeight", ex));
                return 0;
            }            
        }

        public int UpdateFrontWeight(FrontWeight w)
        {
            try
            {
                var cmd = @"UPDATE dbo.FrontWeight
                           SET Eno=@Eno,PLotNo=@PLotNo,PreTime=@PreTime,PreWeight=@PreWeight,Result=@Result,LimitsMin=@LimitsMin,LimitsMax=@LimitsMax
                           WHERE SystemAutoID=@SystemAutoID";
                return DB.ExecuteSqlCommand(cmd,
                     new SqlParameter("@Eno", w.Eno == null ? DBNull.Value : (object)w.Eno),
                     new SqlParameter("@PLotNo", w.PLotNo == null ? DBNull.Value : (object)w.PLotNo),
                     new SqlParameter("@PreTime", w.PreTime == null ? DBNull.Value : (object)w.PreTime),
                     new SqlParameter("@PreWeight", w.PreWeight == null ? DBNull.Value : (object)w.PreWeight),
                     new SqlParameter("@Result", w.Result == null ? DBNull.Value : (object)w.Result),
                     new SqlParameter("@LimitsMin", w.LimitsMin == null ? DBNull.Value : (object)w.LimitsMin),
                     new SqlParameter("@LimitsMax", w.LimitsMax == null ? DBNull.Value : (object)w.LimitsMax),
                     new SqlParameter("@SystemAutoID", w.SystemAutoID)
                     );
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "FrontWeightReporitory", "UpdateFrontWeight", ex));
                return 0;
            }
        }

        public int DeleteFrontWeight(string plotNo)
        {
            try
            {
                var cmd = @"DELETE FrontWeight WHERE PLotNo=@PLotNo";
              return  DB.ExecuteSqlCommand(cmd,
                    new SqlParameter("@PLotNo", plotNo));
            }
            catch (Exception ex)
            {
                log.Error(string.Format("{0} {1} {2}", "FrontWeightReporitory", "DeleteFrontWeight", ex));
                return 0;
            }
            
        }


    }
}
